﻿using FunctionLib.DBOperator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunctionLib.Help
{
    public class FunctionHelp
    {
        public static object ResAdd(int index, object from, object to, object sourceId, object quantityId, DateTime tempDate, string calcType, ref double resNumber, int calcTypeInt, ref int avgCount, ref DateTime time)
        {

            if (calcTypeInt == 6 && resNumber != 0.0)
            {
                return null;
            }
            {
                string checkResT = QueryHelp.NumCheck(from);
                if (checkResT != null)
                {
                    return checkResT + ":" + index.ToString();
                }
            }
            {
                string checkResT = QueryHelp.NumCheck(to);
                if (checkResT != null)
                {
                    return checkResT + ":" + (index + 1).ToString();
                }
            }
            DateTime newTime = default(DateTime);
            object newValue = FunctionHelp.GetTimeRangeResult(from, to, sourceId, quantityId, tempDate, calcType, calcTypeInt, ref newTime);
            string checkRes = QueryHelp.NumCheck(newValue);
            if (checkRes == null)
            {
                double resNum = Convert.ToDouble(newValue);
                if (double.IsNaN(resNum))
                {
                    resNum = 0.0;
                }
                else
                {
                    avgCount++;
                }
                switch (calcTypeInt)
                {
                    case 0:
                        resNumber += resNum;
                        break;
                    case 1:
                        resNumber += resNum;
                        break;
                    case 2:
                        resNumber += resNum;
                        break;
                    case 3:
                        resNumber = ((resNumber < resNum) ? resNumber : resNum);
                        time = ((resNumber < resNum) ? time : newTime);
                        break;
                    case 4:
                        resNumber = ((resNumber > resNum) ? resNumber : resNum);
                        time = ((resNumber > resNum) ? time : newTime);
                        break;
                    case 5:
                        resNumber = resNum;
                        break;
                    case 7:
                        resNumber = ((resNumber < resNum) ? resNumber : resNum);
                        time = ((resNumber < resNum) ? time : newTime);
                        break;
                    case 8:
                        resNumber = ((resNumber > resNum) ? resNumber : resNum);
                        time = ((resNumber > resNum) ? time : newTime);
                        break;
                    case 9:
                        resNumber = ((resNumber < resNum) ? resNumber : resNum);
                        time = ((resNumber < resNum) ? time : newTime);
                        break;
                    case 10:
                        resNumber = ((resNumber > resNum) ? resNumber : resNum);
                        time = ((resNumber > resNum) ? time : newTime);
                        break;
                }
                if (calcTypeInt == 0 || calcTypeInt == 1 || calcTypeInt == 2)
                {
                    resNumber += Convert.ToDouble(newValue);
                }
                return null;
            }
            return checkRes;
        }
        public static object GetTimeRangeResult(object startHour, object endHour, object sourceId, object quantityId, DateTime temp, string calcType, int calcTypeInt, ref DateTime time)
        {
            var timeRange1Start = Convert.ToInt32(startHour);
            var timeRange1End = Convert.ToInt32(endHour);
            return GetTimeRangeResult(sourceId, quantityId, temp.AddHours(timeRange1Start), temp.AddHours(timeRange1End), calcType, calcTypeInt, ref time);
        }

        public static object GetTimeRangeResult(object sourceId, object quantityId, DateTime start, DateTime end, string calcType, int calcTypeInt, ref DateTime time)
        {

            string dateFront = start.ToString("yyyy-MM-dd HH:mm:ss");
            string dateAfter = end.AddHours(1.0).ToString("yyyy-MM-dd HH:mm:ss");
            string equeEndStr = QueryHelp.IsWithEndEque(calcTypeInt) ? "=" : "";
            string queryString = string.Format("select {0} from {1} where time >= '{2}' and time<{3}'{4}' and SourceID = '{5}' and QuantityID = '{6}'", new object[]
            {
                calcType,
                PublicDef.DBTableName,
                dateFront,
                equeEndStr,
                dateAfter,
                sourceId,
                quantityId
            });
            return QueryHelp.GetResult(InfluxDBClient.CreatDBClient().ExecuteQuery(queryString), ref time);
        }

        public static object GetFunctionReturnResult(int calcType, DateTime time, double value)
        {
            switch (calcType)
            {
                case 0:
                case 1:
                case 2:
                case 5:
                case 6:
                case 7:
                case 8:
                    return value;
                case 3:
                case 4:
                    return value.ToString() + ":" + time.ToString("yyyy-MM-dd HH:mm:ss");
                case 9:
                case 10:
                    return time;
                default:
                    return value;
            }
        }
    }
}
